require(['grid', 'topBar', 'highcharts', 'date', 'zTreeCheckbox'], function () {
    var postData;
    var passParams={};
    var $topBar =$('.topBar');

    $('#top').topBar({
        left: [{
            type: 'filter-more',
            search: function () {
                search();
            },
            item: [
                {
                    text: '时间快捷',
                    name1: 'beginTime',
                    name2: 'endTime',
                    hidden:true,
                    type: 'dateLine'
                }, {
                    text: '故障等级',
                    name: 'mftGradeCode',
                    type: 'item-select',
                    data: [
                        {text: '全部', value: ''},
                        {text: '严重', value: '1',selected:true},
                        {text: '警告', value: '3'},
                        {text: '一般', value: '2'},
                        {text: '正常', value: '4'}
                    ]
                }, {
                    text: '修复状态',
                    name: 'isHandle',
                    type: 'item-select',
                    data: [
                        {text: '全部', value: ''},
                        {text: '已修复', value: '0'},
                        {text: '未修复', value: '1',selected:true}
                    ]
                }
            ]
        }
        ],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('Pdf');
                        }
                    }
                ]
            }
        ]
    });
    $('body').css('overflow', 'hidden');

    $('.pm-con').height($(window).height() - $topBar.outerHeight() - 20);

    //获取左侧选择的节点信息
    function getPostData() {
        var rcx = commonFun.getTreeCheckNodeToArgs();
        if (rcx) {
            postData = $.extend(rcx, $("#top").parent().getValues());
        }
        if (!postData.beginTime||!postData.endTime) {
            $.messager.alert("提示", "请选择查询起止时间段", "info");
            return false;
        }
        postData = $.extend(postData, passParams);
        postData.statisType = 'FAULT';
        return rcx;
    }

    function exportFile(suffix) {
        if (getPostData()) {
            var url = "/faultsCount/exportFaultsCount" + suffix + ".htm";
            $.exportFile(url, postData);
        }
    }

    function search() {
        if (getPostData()) {
            $('#grid').grid('setGridParam', {
                page: 1,
                datatype: "json",
                postData: postData
            }).trigger("reloadGrid");

            $.ajaxExt({
                url: "/faultsCount/searchFaultsCountDataForChart.htm",
                data: postData
            }).done(function (result) {
                if (result) {
                    var series = [{
                        type: 'pie',
                        name: '故障统计',
                        data: []
                    }];
                    $.each(result, function (index, item) {
                        series[0].data.push({name: item.ecuMftName, y: item.cnt});
                    });
                    loadPieChart('#chart', '', series);
                }
                else{
                    $('#chart').parent().hide();
                }
            });
        }
    }

    //饼图
    function loadPieChart(divId, text, series) {
        $(divId).parent().show();
        $(divId).highcharts({
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: text
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        format: '<b>{point.name}</b>: {y} 次'
                    },
                    showInLegend: true
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                labelFormatter: function () {
                    return this.name;
                },
                floating: false
            },
            series: series,
            credits: {
                enabled: false
            }
        });
    }

    $('#grid').grid({
        multiselect: false,
        url: "/faultsCount/searchFaultsCount.htm",
        colModel: [
            {name: "ecuMftCode", label: "ecuMftCode", hidden: true},
            {name: "ranking", label: "排名", align: "right",width:40, sortable:true},
            {name: "ecuMftName", label: "故障类型",width:80},
            {name: "cnt", label: "故障总数", align: "right",width:80},
            {name: "carCnt", label: "故障车辆数", align: "right",width:100},
            {name: "rptcnt", label: "已修复(次)", align: "right",width:100},
            {name: "unrptcnt", label: "未修复(次)", align: "right",width:100}
        ]
    });

    $('body').css('overflow', 'inherit');

});